import { reactive } from 'vue'
import { getCenterData, getFrontData, getCardData } from '@/utils/http/homeHttp'
type talk = {
  _id: string
  createTime: string
  talk: string
  username: string
}
export default {
  state: reactive({
    BaseUrl: 'http://47.122.16.71:3999/',
    centerData: [],
    frontData: <{ id: string }>{},
    previewImg: 'frontUploads/bgsmall.jpg',
    userBase: GetUserBase(),
    talkList: <talk[]>[],
    talkNum: <string | number>0,
  }),
  // 获取预览图
  getGetPreviewImg() {
    return this.state.BaseUrl + this.state.previewImg
  },
  // 获取center数据
  GetCenterData() {
    if (this.state.centerData.length) return
    getCenterData().then((res) => {
      this.state.centerData.push(...(res.data.info as []))
    })
  },

  // 获取主页数据 和首页卡片数据
  GetFrontData() {
    if (this.state.frontData.id) return
    getFrontData().then((res) => {
      this.state.frontData = res.data.info[0]
    })
  },

  // 前端登录数据
  SetUserBase(value: {}) {
    this.state.userBase = value
    sessionStorage.setItem('userBase', JSON.stringify(this.state.userBase))
  },
  // 显示登录、
  IsShowCenetr() {
    return (this.state.userBase as { username: string }).username ? true : false
  },
  // 给talk添加数据
  postTalk(arr: talk[], num: string | number) {
    this.state.talkList.push(...arr)
    this.state.talkNum = num
  },
}

// 返回用户数据
function GetUserBase() {
  let obj = {}
  if (sessionStorage.getItem('userBase')) {
    obj = JSON.parse(sessionStorage.getItem('userBase') as string)
  }

  return obj
}
